var app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_go: "../../images/go.png",

    user_info: {
      center_coupon_show: false,
      img_logo: '',
      User_NickName: '',
      sex: ''
    },
    ordernum: '',
    auth_flag: false
  },
  // 登录
  login: function () {
    app.login('', this.load, 'center');
  },
  //获取订单数量
  getordernum() {
    var commitArgs = {
      act: 'get_order_num',
      User_ID: app.globalData.userInfo.User_ID,
    };
    app.http_req(commitArgs, app.globalData.init.api_url, 'POST', this.ordercommit);
  },

  //回调
  ordercommit: function (res) {
    this.setData({
      ordernum: res.data
    })
  },
  //收货地址跳转
  address:function(){
    wx.navigateTo({
      url: '../addresslist/addresslist?from=user_center'
    });
  },

  qiandao() {
    var transData = {
      User_ID: app.globalData.userInfo.User_ID,
      act: 'get_jifen'
    };
    app.http_req(transData, app.globalData.init.api_url, 'POST', this.detail_callback);
  },
  detail_callback(res) {
    wx: wx.showModal({
      title: '提示',
      content: res.msg,
      showCancel: false,
      success: function (res) { },
    })
  },

  //打开页面
  openUrl: function (e) {
    utils.openUrl(e.currentTarget.dataset.url);
  },

  //清理缓存
  clearStorage:function() {
    wx.showToast({
      title: '清理中...',
      icon:'loading'
    })
    wx.clearStorage({
      success:function() {
        wx.showToast({
          title: '清理完成',
        })
      }
    })
  },

  //调取授权管理页面
  openSetting: function () {
    wx.openSetting({
      success: function (res) {
        if (res.authSetting && res.authSetting['scope.userInfo'] && !app.globalData.userInfo) {
          app.login('reLogin');
        }
      }
    });
  },

  //页面加载
  load: function () {
    if (!app.globalData.userInfo) {
      app.login('reLogin', this.load);
      return false;
    }
    if (!app.globalData.shopConfig) {
      //获取商家配置信息
      app.getShopConfig(this.load);
      return false;
    }
    this.setData({
      qiandaozt: app.globalData.shopConfig.signflag,
      'user_info.center_coupon_show': app.globalData.userInfo.center_coupon_show,
      'user_info.img_logo': app.globalData.userInfo.User_HeadImg.length > 0 ? app.globalData.userInfo.User_HeadImg : '../../images/avatar.jpg',
      'user_info.User_NickName': app.globalData.userInfo.User_NickName.length > 0 ? app.globalData.userInfo.User_NickName : '暂无昵称',
      'user_info.sex': app.globalData.userInfo.User_Gender.length > 0 ? app.globalData.userInfo.User_Gender : '未知'
    });
    this.getordernum()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideLoading();

    var that = this;
    wx.getSetting({
      success: function (res) {
        that.setData({
          auth_flag: typeof res.authSetting['scope.userInfo'] != 'undefined' ? true : false
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //检查用户是否登录
    if (!app.globalData.userInfo) {
      this.setData({
        isLogined: false
      })
      return;
    }
    this.setData({
      isLogined: true
    })
    this.load();
  }
})